using UnityEngine;
using System.Collections;


[RequireComponent(typeof(HandPointControl))]
public class Fader : MonoBehaviour {

    // ----- configurable via Unity --------------------------------------------
    public Vector3 direction  = Vector3.left;
    public float size         = 1000.0f;
    public float initialValue = 0.5f;


    // ----- public variables -----------------------------------------------
    public float value { get; private set; }


    // ----- internal variables -----------------------------------------------
    Vector3 start;


    // ----- public methods ---------------------------------------------------
    public void Hand_Update(Vector3 pos) {
        float dot = Vector3.Dot(direction, pos - start);
        value = Mathf.Clamp01(dot / size);
    }

    public void MoveTo(Vector3 pos, float val) {
        start = pos - (direction * (val * size));
    }

    public void MoveToContain(Vector3 pos) {
        float dot = Vector3.Dot(direction, pos - start);
        if (dot > size) {
            start += direction * (dot - size);
        }
        if (dot < 0) {
            start += direction * dot;
        }
    }


    // ----- internal methods -------------------------------------------------
    void Start() {
        value = initialValue;
    }

    void Hand_Create(Vector3 pos) {
        MoveTo(OpenNISessionManager.FocusPoint, initialValue);
        value = initialValue;
    }

    void Hand_Destroy() {
        value = initialValue;
    }
}
